package dynProg.solvers;

import dynProg.Solver;
public class RecursiveSolver implements Solver {

	@Override
	public boolean solve(int[] numbers, int sum) {
		
		int[] numbers1;
		if(numbers.length > 0)
			numbers1 = new int[numbers.length - 1];
		else
			numbers1 = new int[numbers.length];
			
		int sum1 = 0;
		//if sum == 0, then sum - numbers[i] == equal (from the bigger problem)
		if(sum == 0){
			return true;
		}
		//if sum < 0, then numbers[i] from the bigger problem is large than sum.
		else if(sum<0){
			return false;
		}
		//the sum of numbers[i].length == 0, equals the sum when sum = 0.
		else if(sum == 0 && numbers.length == 0)
			return true;
		else if(numbers.length == 0 && sum != 0)
			return false;
	
		/*int sum1 = 0;
		
		else if(numbers.length == 0)return false;
		*/
		//int[] numbers1 = new int[numbers.length-1];
		for(int i = numbers.length - 1; i >= 0; i--){
			//the new sum.
			sum1 = sum - numbers[i];

				//numbers1[j] = numbers[i-1];
			for(int j = 0; j < numbers.length; j++){
				if(j != i){
					if(j > i){
						numbers1[j - 1] = numbers[j];
					}
					else{
						numbers1[j] = numbers[j];
					}
				}
			}
				if(solve(numbers1, sum1)){
					return true;
				}
			/*for(int j = 1; j < numbers.length; j++){
				if(j == i);
				else{
					sum1 += numbers[j];
					if(sum1 < sum){
						//numbers1[j - 1] = numbers[j];
					}
					else if(sum1 == sum){
						return true;
					}
					else{
						sum1 -= numbers[j];
					}
					//numbers1[j - 1] = numbers[j];
					System.out.println("sum1: " + sum1);
					System.out.println("numbers: " + numbers[j]);*/
				
		}
				
		
		
		/*
		for(int i = 1; i < numbers.length; i++){
			sum1 += (numbers[i] - numbers[0]);
			System.out.println(sum1);
			if(sum1 < sum){
				numbers1[i - 1] = numbers[i];
			}
			else if(sum1 == sum){
				return true;
			}
			else{
				sum1 -= (numbers[i] - numbers[0]);
			}
			System.out.println(sum1);
		}
		solve(numbers1, sum1);*/
		return false;
	}

}
